﻿using RuoVea.ExEnum;
using RuoVea.ExSugar;

namespace RuoVea.CodeGen.Web.Controllers;

/// <summary>
/// 
/// </summary>
public partial class FieldMetadataController : Controller
{
    /// <summary>
    /// 
    /// </summary>
    private readonly SugarRepository<GenFieldMetadata> _thisRepository;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="thisRepository"></param> 
    public FieldMetadataController(SugarRepository<GenFieldMetadata> thisRepository)
    {
        _thisRepository = thisRepository;
    }

    public IActionResult Index(string tableId)
    {
        ViewBag.tableId = tableId;
        return View();
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("/FieldMetadata/pages")]
    public async Task<RestfulResult<PageResult<GenFieldMetadata>>> GetPagesAsync()
    {
        using var sr = new StreamReader(Request.Body);
        string dataStr = await sr.ReadToEndAsync();
        if (Request.Body.CanSeek)
            Request.Body.Position = 0;
        if (string.IsNullOrWhiteSpace(dataStr))
            throw new Exception("未提供上传数据。请在Request.Body中携带上传数据。");
        FieldMetadataPageParam? data = JsonConvert.DeserializeObject<FieldMetadataPageParam>(dataStr);
        data ??= new FieldMetadataPageParam { PageNo = 1, PageSize = 2000 };
        var restful = new RestfulResult<PageResult<GenFieldMetadata>>() { Code = CodeStatus.OK };
        var query = await _thisRepository.AsQueryable()
            .Where(x=>x.TableMetaId==data.TableMetaId)
            .Select(x => x )
            .ToPageAsync(data.PageNo, data.PageSize);

        restful.Data = query;
        return restful;
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <returns></returns>
    [HttpGet("/FieldMetadata/list")]
    public async Task<RestfulResult<PageResult<GenFieldMetadata>>> GetlistAsync()
    {
        var restful = new RestfulResult<PageResult<GenFieldMetadata>>() { Code = CodeStatus.OK };
        PageParam data = new PageParam { PageNo = 1, PageSize = 100 };
        var query = await _thisRepository.AsQueryable()
            .ToPageAsync(data.PageNo, data.PageSize);
        restful.Data = query;
        return restful;
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork]
    [HttpPost("/FieldMetadata/add")]
    public async Task<RestfulResult> AddAsync([FromBody] GenFieldMetadata data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };
        bool result = await _thisRepository.InsertAsync(data);
        if (result)
        {
            restful.Data = result;
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Message = "添加失败";
            return restful;
        }
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("/FieldMetadata/update")]
    public async Task<RestfulResult> UpdateAsync([FromBody] GenFieldMetadata data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };
        bool result = await _thisRepository.UpdateAsync(data, true, true, "笔记修改");
        if (result)
        {
            restful.Data = result;
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Message = "修改失败";
            return restful;
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("/FieldMetadata/delete")]
    public async Task<RestfulResult> DeleteAsync([FromBody] EntityBaseId data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };

        if (data.Id == 0)
        {
            restful.Message = "删除失败";
            return restful;
        }
        bool isDelete = await _thisRepository.DeleteByIdAsync(data.Id);
        if (isDelete)
        {
            restful.Data = isDelete;
            restful.Message = "删除成功";
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Data = isDelete;
            restful.Message = "删除失败";
            return restful;
        }
    }
}